package com.base.controller;

import java.io.IOException;
import java.text.ParseException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.base.comm.CommContent;
import com.base.po.voip.ShareLink;
import com.base.service.LogsService;
import com.base.service.ShareLinkService;
import com.base.utils.IP;
import com.base.utils.Produce;

/**
 * 添加分享链接内容服务端
 * 
 * @author
 *
 */
@Controller
@RequestMapping("/admin")
public class add_ShareLink {

	@Autowired
	private LogsService logsService; // log日志
	@Autowired
	private ShareLinkService shareLinkService; // 分享链接信息

	// 添加分享图片
	@RequestMapping(value = "/toAddShareLink", method = RequestMethod.POST)
	public @ResponseBody int toAddShareLink(HttpServletRequest request, ShareLink sharelink) throws IOException, ParseException {
		String userid = (String) request.getSession().getAttribute("adminUserId"); // 登录的用户ID
		if (userid == null || userid.equals("")) return -1; // 未登录
		List<ShareLink> addShareLink = shareLinkService.selectByAll(new ShareLink(userid,(byte)1));
		if (addShareLink.size() >= 1) return -2;// 判断是否存在1条
		sharelink.setId(Produce.getUUID()); // 设置参数 UUID
		sharelink.setCreatetime(Produce.getNowDate()); // 时间
		sharelink.setParentid(userid); // 代理商ID
		sharelink.setDatastatus((byte) 1); // 数据状态

		// 获取域名
		String path = request.getContextPath();
		String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort();
		//二维码中 放入的链接
		String shareUrl = basePath + path + "/login/shareIndex";
        //分享链接
		sharelink.setUrl(shareUrl);
		logsService.insert(CommContent.TYPE_CREATE, CommContent.LOGS_XIAOXI + "配置分享页图片信息",
				(String) request.getSession().getAttribute("userNick"), true, IP.getIpAddr(request));
		return shareLinkService.insertSelective(sharelink); // 添加
	}
}
